package com.maxafaa.moduleframeanimation;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.content.res.AssetManager;
import java.io.InputStream;
import android.view.MotionEvent;

public class MainSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable{

	private boolean m_is_running;
	private SurfaceHolder m_surfaceholder;
	private Paint m_paint;
	private Thread m_mainthread;
	private Canvas m_lockcanvas;
	private Animation m_ryu_animation, m_red_bird_animation, m_yellow_bird_animation, m_blue_bird_animation;
	private Bitmap m_ryu_bitmap, m_birds_bitmap;
	private Bitmap m_ryu_bitmap_transparent;
	private AssetManager m_assetmanager;
	private InputStream m_inputstream;
	
	public MainSurfaceView(Context context) {
		super(context);
		m_surfaceholder = getHolder();
		m_surfaceholder.addCallback(this);		
		setFocusable(true);
		m_assetmanager = context.getAssets();
	}

	private void Initialize(){
		m_paint = new Paint();
		try{
			m_inputstream = m_assetmanager.open("gfx/ryu_anim.png");
			m_ryu_bitmap = BitmapFactory.decodeStream(m_inputstream);
			m_ryu_bitmap_transparent = makeTransparent(m_ryu_bitmap);
			m_ryu_animation = new Animation(m_ryu_bitmap, 77, 65, 68, 66, 13, 0, false, false, false, false, 100);
			m_inputstream = m_assetmanager.open("gfx/bird_anims.png");
			m_birds_bitmap = BitmapFactory.decodeStream(m_inputstream);
			m_red_bird_animation = new Animation(m_birds_bitmap, 41, 124, 34, 52, 3, 0, true, true, true, true, 200);
			m_yellow_bird_animation = new Animation(m_birds_bitmap, 21, 299, 34, 24, 3, 22, false, true, true, false, 20);
			int[] blue_bird_frame_x = new int[3];
			int[] blue_bird_frame_y = new int[3];
			blue_bird_frame_x[0] = 109;
			blue_bird_frame_x[1] = 117;
			blue_bird_frame_x[2] = 41;			
			blue_bird_frame_y[0] = 121;
			blue_bird_frame_y[1] = 35;
			blue_bird_frame_y[2] = 86;
			m_blue_bird_animation = new Animation(m_birds_bitmap, blue_bird_frame_x, blue_bird_frame_y, 34, 24, true);
			
			m_inputstream.close();
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e.toString());
			m_inputstream = null;
			m_ryu_bitmap = null;
			m_ryu_bitmap_transparent = null;
			m_ryu_animation = null;
			m_birds_bitmap = null;
			m_red_bird_animation = null;
			m_yellow_bird_animation = null;
		}
		
		m_mainthread = new Thread(this);
		m_mainthread.start();
		m_is_running = true;
	}
	
	private Bitmap makeTransparent(Bitmap tmpImage)
	{
		int h = tmpImage.getHeight();
		int w = tmpImage.getWidth();
 
		Bitmap resultImage = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
 
		// assume the upperleft corner of the original image is a transparent pixel
		int transparentColor = tmpImage.getPixel(0,0);  
		for (int y=0;y<h;y++)
			for (int x=0;x<w;x++)
			{
				int color=tmpImage.getPixel(x,y);
				if(color==transparentColor)
					color=color & 0x00FFFFFF; // clear the alpha flag
				resultImage.setPixel(x,y,color);
			} 
		return resultImage;
	}
	
	public void GameCreate(){
	}
	
	public void GameStart(){
	}
	
	public void GamePause(){
	}
	
	public void GameResume(){
	}
	
	public void GameStop(){
	}
	
	public void GameDestroy(){
	}
	
	public void GameRestart(){
	}
	
	private void GameUpdate(){
		
	}
	
	private void GameDraw(Canvas canvas){
		canvas.drawColor(Color.WHITE);		
		m_ryu_animation.render(canvas, 50, 50);
		m_red_bird_animation.render(canvas, 130, 150);
		m_yellow_bird_animation.render(canvas, 125, 250);
		m_blue_bird_animation.render(canvas, 165, 400);
		m_paint.setColor(Color.BLACK);
		canvas.drawLine(0, 0, 50, 50, m_paint);
		canvas.drawLine(0, 0, 130, 150, m_paint);
		canvas.drawLine(0, 0, 125, 250, m_paint);
		canvas.drawLine(0, 0, 165, 400, m_paint);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e){
		super.onTouchEvent(e);
		return true;
	}

	@Override
	public void run() {
		while(m_is_running){
			try{
				// Thread.sleep(20);
			}catch(Exception e){}
			GameUpdate();
			m_lockcanvas = m_surfaceholder.lockCanvas(null);
			synchronized (m_surfaceholder) {
				if(m_lockcanvas != null){
					GameDraw(m_lockcanvas);
					m_surfaceholder.unlockCanvasAndPost(m_lockcanvas);
				}
			}
		}		
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		Initialize();		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		boolean retry = true;
		m_is_running = false;
        do {
            try {
                m_mainthread.join();
                retry = false;
            } catch (InterruptedException e) {
				e.printStackTrace();
                // we will try it again and again...
            }
        }while(retry);		
	}
}
